package com.zane.controller.auth;

import com.zane.dto.LoginRequest;
import com.zane.entity.Result;
import com.zane.service.auth.AuthService;
import com.zane.vo.LoginResponse;
import com.zane.vo.RegisterVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 登录接口
     * @param loginRequest
     * @return
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(@RequestBody LoginRequest loginRequest){
        log.info("用户登录：{}",loginRequest);
        if (loginRequest == null){
            log.warn("登录内容不能为空");
            return Result.error("登录内容不能为空");
        }
        LoginResponse res = authService.login(loginRequest);
        if(res == null){
            return Result.error(405,"登录失败");
        }
        return Result.success("登录成功",res);
    }
    @PostMapping("/register")
    public Result<RegisterVO> register(@RequestBody LoginRequest loginRequest){
        log.info("用户注册：{}",loginRequest);
        RegisterVO res = authService.register(loginRequest);
        return Result.success("注册成功");
    }

    /**
     * 登出接口
     * @return
     */
    public Result logout(){
        log.info("用户登出");
        return Result.success("登出成功");
    }
}
